//
//  ZBAppManager.m
//  XZBProduct
//
//  Created by xzb on 2018/7/9.
//  Copyright © 2018年 xzb. All rights reserved.
//

#import "ZBAppManager.h"
#import <ZBPlatform/ZBAppLifecycle.h>
#import <YAMLSerialization.h>

#import "ZBAppManager+Route.h"
#import "ZBAppManager+Lifecycle.h"
#import "ZBAppManager+AppConfig.h"
#import "ZBAppManager+weex.h"
#import "ZBAppManager+Launch.h"
#import "ZBAppManager+Ioc.h"
#import "ZBAppManager+Debug.h"
#import "ZBAppManager+HotFix.h"

@implementation ZBAppManager

ZB_SINGLETON_INSTANCE_METHOD_IMPLEMENTATION

#pragma mark - Life Cycle

- (instancetype)initWithConfig:(NSDictionary *)config
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self registerRoute];
    __weak typeof(self) weakSelf = self;
    [self allLib:^(NSString *lib) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (strongSelf) {
            [strongSelf registerModule:lib];
        }
    }];
    
    
    [self loadingConfig];
    [self registerIoc];
    [self registerDebug];
    [self runHotfixService];
    return YES;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self initWeexSDK];
    [self setupLaunchViewController];
    return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self runHotfixService];
}
- (UIWindow *)keyWindow
{
    return [UIApplication sharedApplication].keyWindow;
}

+ (void)registerModuleNamed:(NSString *)moduleName config:(NSDictionary *)config priority:(NSInteger)priority
{
    Log(@"moduleName: %@",moduleName);
    Log(@"config: %@",config);
    Log(@"priority: %ld",priority);
}

- (void)allLib:(void (^)(NSString *lib))callBlack
{
    kStartTime;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"generate_all_lib" ofType:@""];
    if (path.length < 1) {
        return;
    }
    NSString *content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *libs = [content componentsSeparatedByString:@"./"];
    if (libs.count  < 0) {
        return;
    }
    for (NSString *lib in libs) {
        if ([lib containsString:@"\n"]) {
            NSString *result = [lib stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            if (callBlack) {
                callBlack(result);
            }
        }
    }
    kEnd(@"handle all lib");
}

@end
